/**
 * 
 */
$(function(){
	$(".project").hover3d({
		selector: ".project__card",
		shine: true,
		sensitivity:10,    //设置左上角距离
	});
});



//每次刷新首页时发送请求
$(function(){
	$.get("User_jurisdiction_servlet",function(data,status){
		if(data.result){
			//alert(data.nickname);
			$("#head_photo").removeClass("hide");
			$("#login").addClass("hide");
			$("#register").addClass("hide");
			
			$("#sign_out").removeClass("hide");
			$("#news").removeClass("hide");
			$("#nickname").removeClass("hide");
			$("#nickname").html(data.nickname);
			
			if(data.user_photo_path!=""&&data.user_photo_path!=null){
				$("#user_photo")[0].src=data.user_photo_path;
			}else{
				$("#user_photo")[0].src="images/no_photo.jpg";
			}
		}
	},
	"json"
	);
});



//退出系统
$(function(){
	$("#sign_out").click(function(){
		$.get("Sign_out",function(data,status){
			if(data.result==true){
			window.location.href="index.jsp";	
			}else{
				alert("退出失败");
			}
		},"json");
	});
});




//资讯请求
$(function(){
	$.post("Rss_servlet",function(data,status){
		
		$("#timeline div.timeline-content:eq(0) h2").html(data[0].title);
		$("#timeline div.timeline-content:eq(1) h2").html(data[1].title);
		$("#timeline div.timeline-content:eq(2) h2").html(data[2].title);
		
		$("#timeline div.timeline-content:eq(0) p").html(data[0].description+"(新闻来源腾讯网)");
		$("#timeline div.timeline-content:eq(1) p").html(data[1].description+"(新闻来源腾讯网)");
		$("#timeline div.timeline-content:eq(2) p").html(data[2].description+"(新闻来源腾讯网)");
		
		$("#timeline div.timeline-content:eq(0) a")[0].href=data[0].link;
		$("#timeline div.timeline-content:eq(1) a")[0].href=data[1].link;
		$("#timeline div.timeline-content:eq(2) a")[0].href=data[2].link;
	},"json");
});





////////////////////////----------------------点赞/加关注/转发--------------------////////////
$(function(){
	var $timeline_items = $("div[class='itemdiv commentdiv hidden template_item']")
    var $attention = $timeline_items.find("i.attention");
    var $praise = $timeline_items.find("i.praise")
    var $forword = $timeline_items.find("i.forword")
    
    //加关注
    $attention.click(function(){
    	var $attention_number = $(this).siblings("span.attention");
    	var attention_number = parseInt($attention_number.html());
    	var $dynamic = $(this).parents("div[class='itemdiv commentdiv template_item']");
    	$.ajax({
    		url:"Attention",
    		type:"POST",
    		async:false,
    		data:{dynamic_information_id:$dynamic[0].id},
    		dataType:"json",
    		success:function(data,status){
    			if(data.result==true){
    				 if(data.attentioned==true){
    				 $attention_number.html(attention_number+1);
    				 }else{
    					 jError("您已经关注过该动态了",{HorizontalPosition : "center",VerticalPosition : "center",ShowOverlay : false});
    				 }
    			}else{
    				jError("操作失败",{HorizontalPosition : "center",VerticalPosition : "center",ShowOverlay : false});
    			}
    		}
    	});
    });
    
    //点赞
    $praise.click(function(){
    	var $praise_number = $(this).siblings("span.praise");
    	var praise_number = parseInt($praise_number.html());
    	var $dynamic = $(this).parents("div[class='itemdiv commentdiv template_item']");
    	
    	$.ajax({
    		url:"Praise",
    		type:"POST",
    		async:false,
    		data:{dynamic_information_id:$dynamic[0].id},
    		dataType:"json",
    		success:function(data,status){
    			if(data.result==true){
    				 if(data.praise==true){
    					 $praise_number.html(praise_number+1);
    				 }else{
    					 jError("您已经点过赞了",{HorizontalPosition : "center",VerticalPosition : "center",ShowOverlay : false});
    				 }
    			}else{
    				jError("操作失败",{HorizontalPosition : "center",VerticalPosition : "center",ShowOverlay : false});
    			}
    		}
    	});
    });
});



//////////////-------------------热门动态---------------------//////////
/*
 * 筛选动态，并插入页面
 * data 数据
 * start  开始位置
 * end   结束位置
 * type 0-今日 ,1-一周
 */
function dynamic_screen(data , start , end , type){
	
	var $news_item = $("div[class='itemdiv commentdiv hidden template_item']");
	var $24hours = $("#24hours");
	var $week = $("#week");
	
	if((end-data.length)>5){
		end = 5;
	}
	if(end>data.length){
		end = data.length;
	}
	if((end-start)!=5){
		start = end - 5;
	}
	if(end<5){
		start = 0;
	}
	
	
	
	for(i = start;i<end;i++){
		var $item = $news_item.clone(true);
		$item[0].id = data[i].information_id;
		if(data[i].user_photo_url == "" || data[i].user_photo_url == null){
			$item.find("img.dy_user")[0].src = "images/no_photo.jpg";
		}else{
		    $item.find("img.dy_user")[0].src = data[i].user_photo_url;
		}
		$item.find("a.dy_user_nickname").html(data[i].user_nickname);
		$item.find("span.dy_date").html(data[i].date);
		$item.find("span.dy_text").html(data[i].content);
		
		$item.find("span.attention").html(data[i].attention_number);
		$item.find("span.praise").html(data[i].praise_number);
		$item.find("span.review").html(data[i].review_number);
		$item.find("span.forword").html(data[i].forword_number);
		
		$item.removeClass("hidden");
		if(type == 0){
			$24hours.prepend($item);
		}else if(type == 1){
		    $week.prepend($item);
		}
		
	}
}



$(function(){

	
	var today_dynamic;
	var week_dynamic;
	$.get("Hot_informations",function(data,status){
		if(data.result==true){
			today_dynamic = data.dynamic_informations_24hours;
			week_dynamic = data.dynamic_informations_week;
			//alert(data.dynamic_informations_24hours[0].information_id);
			dynamic_screen(today_dynamic , 0 , 5 , 0);  //今日热门
			dynamic_screen(week_dynamic , 0 , 5 , 1);   //本周热门 
			
			
		}else{
			alert("数据请求失败");
		}
		
		
		
	},"json");
		
});
